﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CuaHangMayTinh.BusinessEntities;
using CuaHangMayTinh.DataProvider;
using CuaHangMayTinh.Utilities;
using System.Data;

namespace CuaHangMayTinh.DataAccessLayer
{
    /// <summary>
    /// Lớp SubGroupUserDA
    /// </summary>
    public class SubGroupUserDA
    {

        public SubGroupUserDA()
        {

        }

        /// <summary>
        /// Hàm thêm 1 SubGroupUserEntity mới
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public int Insert(SubGroupUserEntity entity)
        {
            try
            {
                ParameterBuilder pb = DBFactory.CreateParamBuilder();
                pb.AddParameter("Name", entity.Name);
                pb.AddParameter("IDGU", entity.IDGU);

                return (int)DBFactory.Database.ExecuteNonQuery("SubGroupUser_Insert", pb.Parameters);
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                throw new Exception(ex.Message, ex);
            }
        }

        /// <summary>
        /// Hàm thêm 1 SubGroupUserEntity mới, trả về ID mới thêm vào
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public int InsertGetID(SubGroupUserEntity entity)
        {
            try
            {
                ParameterBuilder pb = DBFactory.CreateParamBuilder();

                pb.AddParameter("Name", entity.Name);
                pb.AddParameter("Name", entity.IDGU);
                pb.AddParameter("MoTa", entity.MoTa);

                return (int)DBFactory.Database.ExecuteNonQueryGetID("SubGroupUser_Insert", pb.Parameters);
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                throw new Exception(ex.Message, ex);
            }
        }

        /// <summary>
        /// Hàm Cập nhật 1 SubGroupUserEntity
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool UpDate(SubGroupUserEntity entity)
        {
            try
            {
                ParameterBuilder pb = DBFactory.CreateParamBuilder();
                pb.AddParameter("IDSubGroupUser", entity.IDSubGroupUser);
                pb.AddParameter("Name", entity.Name);
                pb.AddParameter("IDGU", entity.IDGU);
                pb.AddParameter("MoTa", entity.MoTa);

                int numRowEffected = DBFactory.Database.ExecuteNonQuery("SubGroupUser_Update", pb.Parameters);
                return (numRowEffected > 0);
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                throw new Exception(ex.Message, ex);
            }
        }

        /// <summary>
        /// Hàm tìm tất cả SubGroupUserEntity
        /// </summary>
        /// <returns></returns>
        public DataTable Find_All()
        {
            try
            {
                DataTable dt = new DataTable();
                ParameterBuilder pb = DBFactory.CreateParamBuilder();

                dt = DBFactory.Database.FillDataTable("SubGroupUser_FindAll", pb.Parameters);
                return dt;
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                throw new Exception(ex.Message, ex);
            }
        }

        /// <summary>
        /// Hàm tìm SubGroupUserEntity bằng ID
        /// </summary>
        /// <param name="IDSubGroupUser"></param>
        /// <returns></returns>
        public DataTable Find_ID(int IDSubGroupUser)
        {
            try
            {
                DataTable dt = new DataTable();
                ParameterBuilder pd = DBFactory.CreateParamBuilder();
                pd.AddParameter("IDSubGroupUser", IDSubGroupUser);

                dt = DBFactory.Database.FillDataTable("SubGroupUser_FindID", pd.Parameters);
                return dt;
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                throw new Exception(ex.Message);
            }
        }

        /// <summary>
        /// Hàm tìm SubGroupUser bằng ID của GroupUser
        /// </summary>
        /// <param name="IDGroupUser"></param>
        /// <returns></returns>
        public DataTable Find_IDGroupUser(int IDGroupUser)
        {
            try
            {
                DataTable dt = new DataTable();
                ParameterBuilder pd = DBFactory.CreateParamBuilder();
                pd.AddParameter("IDGroupUser", IDGroupUser);

                dt = DBFactory.Database.FillDataTable("SubGroupUser_FindIDGroupUser", pd.Parameters);
                return dt;
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                throw new Exception(ex.Message);
            }
        }

        /// <summary>
        /// Hàm tìm SubGroupUser bằng tên
        /// </summary>
        /// <param name="Name"></param>
        /// <returns></returns>
        public DataTable Find_Ten(string Name)
        {
            try
            {
                DataTable dt = new DataTable();
                ParameterBuilder pd = DBFactory.CreateParamBuilder();
                pd.AddParameter("Name", Name);

                dt = DBFactory.Database.FillDataTable("SubGroupUser_Find_Ten", pd.Parameters);
                return dt;
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                throw new Exception(ex.Message);
            }
        }

        /// <summary>
        /// Hàm tìm ID của 1 SubGroupUser
        /// </summary>
        /// <param name="name">Tên của SubGroupUser</param>
        /// <returns></returns>
        public int GetID(string name)
        {
            try
            {
                ParameterBuilder pb = DBFactory.CreateParamBuilder();
                pb.AddParameter("Name", name);

                int kq = (int)DBFactory.Database.ExecuteScalar("SubGroupUser_GetID", pb.Parameters);
                return kq;
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                throw new Exception(ex.Message, ex);
            }
        }

        /// <summary>
        /// Hàm xóa 1 SubGroupUser
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool Delete(int id)
        {
            try
            {
                ParameterBuilder pd = DBFactory.CreateParamBuilder();
                pd.AddParameter("MaNghiepVu", id);

                return DBFactory.Database.ExecuteNonQuery("SubGroupUser_Delete", pd.Parameters) > 0;
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                throw new Exception(ex.Message, ex);
            }
        }

        /// <summary>
        /// Hàm chuyển 1 DataTable thành List SubGroupUser
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public List<SubGroupUserEntity> ConvertToList(DataTable dt)
        {
            List<SubGroupUserEntity> list = new List<SubGroupUserEntity>();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                int idsub = (int)dt.Rows[i][0];
                string name = dt.Rows[i][1].ToString();
                int idgu = (int)dt.Rows[i][2];
                string mota = dt.Rows[i][3].ToString();

                SubGroupUserEntity a = new SubGroupUserEntity(idsub, name, idgu, mota);
                list.Add(a);
            }
            return list;
        }

    }
}

